GET, POST, PUT, DELETE, PATCH রিকোয়েস্ট এর ব্যবহার

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - HTTP Methods এবং Request Handling
148

Spring Boot-এ RestTemplate এবং WebClient ব্যবহার করে GET, POST, PUT, DELETE, এবং PATCH রিকোয়েস্ট পরিচালনা করা যায়। এখানে প্রতিটি রিকোয়েস্টের উদাহরণ দেওয়া হলো:


1. GET Request

GET রিকোয়েস্ট সাধারণত ডেটা রিট্রিভ করার জন্য ব্যবহৃত হয়।

RestTemplate দিয়ে:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    @Autowired
    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String fetchData() {
        String url = "https://api.example.com/data";
        return restTemplate.getForObject(url, String.class);
    }
}

2. POST Request

POST রিকোয়েস্ট নতুন ডেটা তৈরি করার জন্য ব্যবহৃত হয়।

RestTemplate দিয়ে:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String createData(Object requestData) {
        String url = "https://api.example.com/data";
        return restTemplate.postForObject(url, requestData, String.class);
    }
}

3. PUT Request

PUT রিকোয়েস্ট বিদ্যমান ডেটা আপডেট করার জন্য ব্যবহৃত হয়।

RestTemplate দিয়ে:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void updateData(Long id, Object updatedData) {
        String url = "https://api.example.com/data/" + id;
        restTemplate.put(url, updatedData);
    }
}

4. DELETE Request

DELETE রিকোয়েস্ট বিদ্যমান ডেটা মুছে ফেলার জন্য ব্যবহৃত হয়।

RestTemplate দিয়ে:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void deleteData(Long id) {
        String url = "https://api.example.com/data/" + id;
        restTemplate.delete(url);
    }
}

5. PATCH Request

PATCH রিকোয়েস্ট আংশিক আপডেট করার জন্য ব্যবহৃত হয়।

RestTemplate দিয়ে:

RestTemplate সরাসরি PATCH রিকোয়েস্ট সমর্থন করে না, তবে exchange() ব্যবহার করে এটি করা সম্ভব।

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String patchData(Long id, Object partialUpdate) {
        String url = "https://api.example.com/data/" + id;
        HttpEntity<Object> requestEntity = new HttpEntity<>(partialUpdate);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class);
        return response.getBody();
    }
}

WebClient ব্যবহার করে:

WebClient দিয়ে GET, POST, PUT, DELETE, PATCH রিকোয়েস্ট খুব সহজে করা যায়।

GET Example:

public Mono<String> fetchData() {
    return webClient.get()
            .uri("/data")
            .retrieve()
            .bodyToMono(String.class);
}

POST Example:

public Mono<String> createData(Object requestData) {
    return webClient.post()
            .uri("/data")
            .bodyValue(requestData)
            .retrieve()
            .bodyToMono(String.class);
}

PUT Example:

public Mono<Void> updateData(Long id, Object updatedData) {
    return webClient.put()
            .uri("/data/{id}", id)
            .bodyValue(updatedData)
            .retrieve()
            .bodyToMono(Void.class);
}

DELETE Example:

public Mono<Void> deleteData(Long id) {
    return webClient.delete()
            .uri("/data/{id}", id)
            .retrieve()
            .bodyToMono(Void.class);
}

PATCH Example:

public Mono<String> patchData(Long id, Object partialUpdate) {
    return webClient.patch()
            .uri("/data/{id}", id)
            .bodyValue(partialUpdate)
            .retrieve()
            .bodyToMono(String.class);
}

সংক্ষেপে:

  1. RestTemplate: সিনক্রোনাস অপারেশনের জন্য ব্যবহৃত হয়।
  2. WebClient: রিঅ্যাক্টিভ প্রোগ্রামিংয়ের জন্য উপযোগী।

আপনার অ্যাপ্লিকেশনের প্রয়োজন অনুসারে GET, POST, PUT, DELETE এবং PATCH রিকোয়েস্টগুলো সহজেই পরিচালনা করা যায়।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...